$(function() {
		
	// 定义列
	var columns = [
		{
				field : 'no',
				title : '序号',
				align : 'left',
				formatter : function(value, row, index) {
					return index + 1;
				},
				width:"5%"
		},
		{
			field : 'imagePath',
			title : '商品图片',
			align : 'center',
			formatter : function(value, row, index) {
				return "<div class='order_detail_product_image'>"+
				        	"<img src='"+imgRootURL+row.imagePath+"' />"+
				        "</div>";
			},
			width:"10%"
		},
		{
				field : 'productName',
				title : '商品名称',
				align : 'center',
				formatter : function(value, row, index){
					var spec_str = "";
					if(row.productSpecList && row.productSpecList.length > 0){
						spec_str = "【"
						for(var j = 0 ;j < row.productSpecList.length; j++){
							var spec = row.productSpecList[j];
							spec_str += spec.specName+":"+ spec.specValue + " ";
						}
					}
					if(spec_str != ""){
						spec_str += "】";
					}
					return value+"<br>"+spec_str;
				},
				width:"35%"
		},
		{
				field : 'productPrice',
				title : '商品价格',
				align : 'center',
				formatter : function(value, row, index) {
					return "原价：￥<span id='productPrice'>"+row.productPrice+"</span>";
				},
				width:"15%"
		},
		{
				field : 'productAdjustPrice',
				title : '调价',
				align : 'center',
				formatter : function(value, row, index) {
					return "<div class='col-md-12 col-sm-12 col-xs-12'>" +
					"<input type='hidden' name='id' value='"+row.id+"'>" +
					"<input type='text' name='adjustProductPrice' class='form-control col-md-6 col-xs-6' value='"+row.productAdjustPrice+"' onkeyup='updateProductPrice("+index+",\"adjustProductPrice\")'  >" +
					"</div>";
				},
				width:"15%"
		},
		{
				field : 'buyNum',
				title : '购买数量',
				align : 'center',
				formatter : function(value, row, index) {
					return "<span id=\"buyNum\">"+row.buyNum+"</span>";
				},
				width:"10%"
		},
		{
			field : 'productItemMoney',
			title : '金额小计',
			align : 'center',
			formatter : function(value, row, index) {
				return "<span id=\"productItemMoney\">"+row.productItemMoney+"</span>";
			},
			width:"20%"
		}];
			
			
	// 定义查询回调函数（查询参数在此设置）
	var queryParams = function(params) {
	
		var orderCode =  $("#orderCode").val()
		return {
			rows : params.limit, // 页面大小
			page : params.pageNumber, // 页码
			orderCode : orderCode
		};
	};
	
	var url = rootPath + "/orderDetail/listOrderDetailByOrderCode"; // 请求json数据的服务器地址
	sys.initTable('orderProductDetailTable', {
		url : url,
		queryParams : queryParams,
		columns : columns,
		striped : true,
		clickToSelect : true,
		pagination:false
	});
	
	var index = parent.layer.getFrameIndex(window.name);
	parent.layer.iframeAuto(index);

	// 调价保存
	$('#btn_confirm_adjust_price').on('click', function() {
		
		// 提交订单号  商品调价  订单调价  运费
		
		// 订单号
		var orderCode = $("#orderCode").val();
		var produtAdjustPrice = [];
		var ids = [];
		var buyNums = [];
		var produtPrice = [];
		
		// 商品调价
		$("input[name='adjustProductPrice']").each(function(i,item){
			produtAdjustPrice.push($(this).val());
		});
		
		// 商品ids
		$("input[name='id']").each(function(i,item){
			ids.push($(this).val());
		});
		
		// 购买数量
		$("span[id='buyNum']").each(function(i,item){
			buyNums.push($(this).text());
		});
		
		// 商品原价
		$("span[id='productPrice']").each(function(i,item){
			produtPrice.push($(this).text());
		});
		
		// 运费
		var deliveryFeeMoney = $("#deliveryFeeMoney").val();
		// 订单调价
		var orderAdjustMoney = $("#orderAdjustPrice").val();
		// 调价原因
		var adjustReason = $("#adjustReason").val();
		
		var order_real_pay = parseFloat($("#orderRealPayMoneyShow").text());
		var flag = $("#orderRealPayMoneyShowLabel").text();
		
		if(flag == "-￥"){
			showErr("调价后订单应付金额不能为负");
			return false;
		}
		
		var params = {
			orderCode : orderCode,
			ids : ids.join(','),
			produtAdjustPrice: produtAdjustPrice.join(','),
			produtPrice : produtPrice.join(','),
			deliveryFeeMoney: deliveryFeeMoney,
			orderAdjustMoney: orderAdjustMoney,
			buyNums: buyNums.join(','),
			adjustReason : adjustReason
		};
		
		var url = rootPath + "/order/adjustOrderPrice";
		$.ajax({
	        type:"post",
	        url:url,
	        data:params,
	        datatype: "json",
	        success:function(json){
	        	if(json.resultCode == '1'){
	        		var index = parent.layer.getFrameIndex(window.name);
	        		parent.layer.close(index);
	        		parent.$("#orderTable").bootstrapTable("refresh");
	        		parent.layer.alert('调价成功',{icon: 6});
	        	}
	        },
	        error: function(){
	        	showErr(json.resultMsg);
	        }
		});
		
	});

})


// 调整订单商品价格
function updateProductPrice(index,field){
	// 更新商品小计
	
	var curInput = $("input[name='"+field+"']:eq("+index+")");
	// 用户输入的调价
	var value = curInput.val();
	// alert("当前输入调整的价格："+value);
	if(value == undefined || $.trim(value) == "" || value == null){
		curInput.val(0);
		value = 0;
	}
	
	// 原价
	var curProductPriceSpan =  $("span[id='productPrice']:eq("+index+")");
	var curProductPrice = curProductPriceSpan.text();
	// alert("原价:"+curProductPrice);
	
	// 购买数量
	var curBuyNumSpan =  $("span[id='buyNum']:eq("+index+")");
	curBuyNum = curBuyNumSpan.text();
	// alert("购买数量："+curBuyNum);
	
	// 调整后的价格
	var adjustPrice = (parseFloat(value)*100) * parseFloat(curBuyNum) / 100;
	var curBuyNumSpan =  $("span[id='productItemMoney']:eq("+index+")");
	// 改变每条商品的金额小计
	curBuyNumSpan.text(numberRand2(adjustPrice));
	// alert("商品小计:"+numberRand2(adjustPrice));
	
	var totalProductAdjustPrice = 0;
	// 更新商品调价总计
	$("input[name='adjustProductPrice']").each(function(i,item){
		var product_price = $("span[id='productPrice']:eq("+i+")").text();
		var buyNum = parseFloat($("span[id='buyNum']:eq("+i+")").text());
		// (原价 - 调价) * 数量
		totalProductAdjustPrice += ( (parseFloat(product_price)*100) - (parseFloat($(this).val())*100) )   * buyNum / 100;
	});
	$("#totalProductAdjustMoneyShow").text(totalProductAdjustPrice.toFixed(2));
	// alert("商品差价:"+totalProductAdjustPrice.toFixed(2));
	
	// 更新商品总计
	var totalProductMoney = 0;
	$("span[id='productItemMoney']").each(function(i,item){
		totalProductMoney += parseFloat($(this).text());
	});
	$("#totalProductMoneyShow").text(totalProductMoney.toFixed(2));
	
	// alert("商品总价："+totalProductMoney);
	// 重新算实付金额  商品金额 - 优惠金额 - 优惠券金额  - 积分抵扣金额 
	var real_pay_money = parseFloat(totalProductMoney) - parseFloat($("#orderPointMoney").val())
		- parseFloat($("#orderCouponMoney").val()) - parseFloat($("#orderDiscountMoney").val());
	
	// 更新订单总金额
	// 调价后应付金额 + 订单总额调降 + 运费
	var totalRealPayMoney = real_pay_money + parseFloat($("#orderAdjustPrice").val()) + parseFloat( $("#deliveryFeeMoney").val());
	
	var resultMoney  = numberRand2(totalRealPayMoney);
	if(resultMoney >= 0){
		$("#orderRealPayMoneyShowLabel").text("￥");
		$("#orderRealPayMoneyShow").text(resultMoney);
	}else{
		$("#orderRealPayMoneyShowLabel").text("-￥");
		$("#orderRealPayMoneyShow").text(resultMoney * -1);
	}
	
}

//订单总额调降
function updateOrderMoney(){
	var value = $("#orderAdjustPrice").val();
	if(value == undefined || $.trim(value) == "" || value == null){
		$("#orderAdjustPrice").val(0);
		value = 0;
	}
	
	var totalProductMoney = 0;
	$("span[id='productItemMoney']").each(function(i,item){
		totalProductMoney += parseFloat($(this).text());
	});
	// 重新算实付金额  商品金额 - 优惠金额 - 优惠券金额  - 积分抵扣金额 
	var real_pay_money = parseFloat(totalProductMoney) - parseFloat($("#orderPointMoney").val())
		- parseFloat($("#orderCouponMoney").val()) - parseFloat($("#orderDiscountMoney").val());
	
	// 调价后应付金额 - 订单总额调降 + 运费
	var totalRealPayMoney = real_pay_money + parseFloat(value)
		+ parseFloat( $("#deliveryFeeMoney").val());
	
	
	var resultMoney  = numberRand2(totalRealPayMoney);
	if(resultMoney >= 0){
		$("#orderRealPayMoneyShowLabel").text("￥");
		$("#orderRealPayMoneyShow").text(resultMoney);
	}else{
		$("#orderRealPayMoneyShowLabel").text("-￥");
		$("#orderRealPayMoneyShow").text(resultMoney * -1);
	}
}


//调整订单运费
function updateOrderDeliveryFeeMoney(){
	
	var value = $("#deliveryFeeMoney").val();
	if(value == undefined || $.trim(value) == "" || value == null){
		$("#deliveryFeeMoney").val(0);
		value = 0;
	}
	var totalProductMoney = 0;
	$("span[id='productItemMoney']").each(function(i,item){
		totalProductMoney += parseFloat($(this).text());
	});
	// 重新算实付金额  商品金额 - 优惠金额 - 优惠券金额  - 积分抵扣金额 
	var real_pay_money = parseFloat(totalProductMoney) - parseFloat($("#orderPointMoney").val())
		- parseFloat($("#orderCouponMoney").val()) - parseFloat($("#orderDiscountMoney").val());
	
	// 调价后应付金额  + 订单总额调降 + 运费
	var totalRealPayMoney = real_pay_money
	+ parseFloat($("#orderAdjustPrice").val())
	+ parseFloat(value);
	
	
	var resultMoney  = numberRand2(totalRealPayMoney);
	if(resultMoney >= 0){
		$("#orderRealPayMoneyShowLabel").text("￥");
		$("#orderRealPayMoneyShow").text(resultMoney);
	}else{
		$("#orderRealPayMoneyShowLabel").text("-￥");
		$("#orderRealPayMoneyShow").text(resultMoney * -1);
	}
}




